<?php
/**
 * Created by PhpStorm.
 * User: Kyle
 * Date: 2019/7/17
 * Time: 15:17
 */

namespace app\common\model;


use think\Exception;
use think\Model;

class Orders extends Model
{
    protected $pk = 'order_id';

    public function topic(){
        return $this->hasOne('Topic','topic_id','property_ids');
    }

    public function user(){
        return $this->hasOne('User','user_id','user_id');
    }

    //下单存库
    public function addOrder($userInfo,$orderInfo,$topic){
        $order = new self();
        $insertOrder['user_id'] = $userInfo['user_id'];
        $insertOrder['order_sn'] = $orderInfo['out_order_no'];
        $insertOrder['amount'] = $topic['tt_play_price'];
        $insertOrder['property_ids'] = $topic['topic_id'];
        $insertOrder['order_time'] = time();
        $insertOrder['createed_time'] = time();
        $insertOrder['updated_time'] = time();
        $insertOrder['status'] = 1;
        $insertOrder['fir_distribution'] = $topic['fir_distribution'];
        $insertOrder['sec_distribution'] = $topic['sec_distribution'];

        $save = $order->allowField(true)->save($insertOrder);
        if(!$save){throw  new Exception('下单存储数据存储失败');}
        return $save;
    }
}